package com.zm.demo.core.sort;

import java.util.Arrays;
import java.util.Random;

import static java.lang.System.out;

/**
 * @author zoum
 * @create 2020/3/10 13:17
 */
public class QuickSort3 {

    private static Random random = new Random();

    public static void main(String[] args){
        int[] s = new int[new Random().nextInt(20)];
        for (int i = 0; i < s.length; i++) {
            s[i] = new Random().nextInt(1000);
        }


        out.println(Arrays.toString(s));
        out.println(s.length);
        quickSort(s, 0, s.length-1);
        out.println(Arrays.toString(s));

    }

    public static void quickSort(int[] source, int start, int end) {
        if (source == null || source.length == 0){
            return;
        }

        int i = start;
        int j = end;
        int tmp = source[start];

        while (i<j){
            while (source[i]<tmp && i<j){
                i++;
            }
            while (source[j]>tmp && i<j){
                j--;
            }
            if (source[i] == source[j] && i<j){
                i++;
            }else {
                int tmp1 = source[i];
                source[i] = source[j];
                source[j] = tmp1;
            }
        }
        if (i-1 > start){
            quickSort(source, start, i-1);
        }
        if (j+1 < end){
            quickSort(source, j+1, end);
        }
    }
}
